package com.huawei.hwCloudJs.support.a;

import android.util.Log;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

public final class b {
    private static final b c = new b();
    private static HashMap<String, a> d = new LinkedHashMap(100) {
    };
    private long a = 10000000;
    private long b = 0;

    private b() {
    }

    public static b a() {
        return c;
    }

    public synchronized a a(String str) {
        return (a) d.get(str);
    }

    public void a(long j) {
        this.a = j;
    }

    public synchronized void a(String str, a aVar) {
        a aVar2 = (a) d.get(str);
        if (aVar2 != null) {
            this.b -= (long) aVar2.a();
        }
        d.put(str, aVar);
        this.b += (long) aVar.a();
        if (this.b > this.a) {
            for (Entry key : d.entrySet()) {
                e((String) key.getKey());
                if (this.b < this.a / 2) {
                    break;
                }
            }
        }
    }

    public long b() {
        return this.a;
    }

    public synchronized a b(String str) {
        a a;
        if (d(str)) {
            a = a(str);
            if (a.e() == 0) {
                this.b -= (long) a.a();
                d.remove(str);
            }
        } else {
            a = null;
        }
        return a;
    }

    public synchronized a c(String str) {
        a b;
        b = b(str);
        if (b != null) {
            Log.i("CacheManager", "getCacheInfo success!");
        } else {
            try {
                URL url = new URL(str);
                String str2 = (url.getPath().isEmpty() || url.getPath().equalsIgnoreCase("/")) ? url.getProtocol() + "://" + url.getAuthority() : url.getProtocol() + "://" + url.getAuthority() + url.getPath();
                b = b(str2);
            } catch (MalformedURLException e) {
                Log.e("CacheManager", "MalformedURLException");
                b = null;
            }
        }
        return b;
    }

    public synchronized void c() {
        d.clear();
        this.b = 0;
    }

    public synchronized int d() {
        return d.size();
    }

    public synchronized boolean d(String str) {
        return d.containsKey(str);
    }

    public synchronized long e() {
        return this.b;
    }

    public synchronized void e(String str) {
        a a = a(str);
        if (a != null) {
            this.b -= (long) a.a();
        }
        d.remove(str);
    }
}
